package com.bamboo.eduservice.service.impl;

import com.bamboo.eduservice.entity.EduCourse;
import com.bamboo.eduservice.entity.EduTeacher;
import com.bamboo.eduservice.entity.vo.TeacherQuery;
import com.bamboo.eduservice.mapper.EduTeacherMapper;
import com.bamboo.eduservice.service.EduCourseService;
import com.bamboo.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author jack.wu
 * @version 1.0
 * @date 2020-06-30
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Resource
    private EduCourseService courseService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Page<EduTeacher> pageTeacherCondition(long current, long limit, TeacherQuery teacherQuery) {
        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if (!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");
        page(pageTeacher, wrapper);
        return pageTeacher;
    }

    @Override
    public Map<String, Object> getFrontTeacherList(long page, long limit) {
        Map<String, Object> result = new HashMap<>(10);
        Page<EduTeacher> pageTeacher = new Page<>(page, limit);
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort", "gmt_create");
        baseMapper.selectPage(pageTeacher, queryWrapper);

        List<EduTeacher> records = pageTeacher.getRecords();
        long current = pageTeacher.getCurrent();
        long pages = pageTeacher.getPages();
        long size = pageTeacher.getSize();
        long total = pageTeacher.getTotal();
        boolean hasNext = pageTeacher.hasNext();
        boolean hasPrevious = pageTeacher.hasPrevious();

        //把分页数据获取出来，放到map集合
        result.put("items", records);
        result.put("current", current);
        result.put("pages", pages);
        result.put("size", size);
        result.put("total", total);
        result.put("hasNext", hasNext);
        result.put("hasPrevious", hasPrevious);
        return result;
    }

    @Override
    public Map<String, Object> getTeacherInfo(String id) {
        Map<String, Object> result = new HashMap<>(3);
        // 查询讲师
        EduTeacher eduTeacher = baseMapper.selectById(id);
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id", id);
        List<EduCourse> courseList = courseService.list(queryWrapper);
        result.put("teacher", eduTeacher);
        result.put("courseList", courseList);
        return result;
    }


}
